package com.zxk.service;

import com.zxk.app.vo.AppStartVo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 任务进度信息
 *
 * @author: 舟小亢
 * @date: 2024-06-16 19:59
 */
public class TaskProgressService {

    /**
     * 线程名，缓存的任务进度信息
     */
    private static Map<String, StringBuilder> hwndProgressBufferMap = new ConcurrentHashMap<>();

    /**
     * 线程名，任务启动时间
     */
    private static Map<String, Long> hwndStartTimeMap = new ConcurrentHashMap<>();

    private static String getCurrentTime() {
        // 当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(System.currentTimeMillis());
        return format;
    }

    /**
     * 更新任务进度
     */
    public static void updateProgress(String taskInfo, String progress) {
        String hwdnName = Thread.currentThread().getName();
        StringBuilder progressBuffer = new StringBuilder();
        // 拼接任务信息
        progressBuffer.append(String.format("%-25s", getCurrentTime()));
        progressBuffer.append(String.format("%-25s", hwdnName));
        progressBuffer.append(String.format("%-15s", taskInfo));
        progressBuffer.append(String.format("%-15s", progress));
        Long startTime = hwndStartTimeMap.get(hwdnName);
        long costTimeLong = System.currentTimeMillis() - startTime;
        double costTimeMin = costTimeLong / 1000 / 60;
        progressBuffer.append(String.format("%-15s", costTimeMin + "分钟"));
        progressBuffer.append("NA");
        hwndProgressBufferMap.put(Thread.currentThread().getName(), progressBuffer);
    }

    /**
     * 获取任务进度
     */
    public static String getProgress() {
        StringBuilder stringBuilderTotal = new StringBuilder();
        stringBuilderTotal.append(String.format("%-25s", "记录时间"));
        stringBuilderTotal.append(String.format("%-25s", "线程名"));
        stringBuilderTotal.append(String.format("%-15s", "当前任务"));
        stringBuilderTotal.append(String.format("%-15s", "任务进度"));
        stringBuilderTotal.append(String.format("%-15s", "总任务耗时"));
        stringBuilderTotal.append(String.format("%-15s", "终止时间"));
        stringBuilderTotal.append("\r\n");
        stringBuilderTotal.append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        stringBuilderTotal.append("\r\n");
        for (Map.Entry<String, StringBuilder> stringStringBuilderEntry : hwndProgressBufferMap.entrySet()) {
            stringBuilderTotal.append(stringStringBuilderEntry.getValue());
            stringBuilderTotal.append("\r\n");
        }
        stringBuilderTotal.append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        stringBuilderTotal.append("\r\n");
        return stringBuilderTotal.toString();
    }

    public static void updateEndProgress() {
        String hwdnName = Thread.currentThread().getName();
        String currentTime = getCurrentTime();
        StringBuilder stringBuilder = hwndProgressBufferMap.getOrDefault(hwdnName, new StringBuilder());
        if (stringBuilder.length() >= 2) {
            stringBuilder.delete(stringBuilder.length() - 2, stringBuilder.length());
        }
        stringBuilder.append(String.format("%-15s", currentTime));
    }

    public static void updateStartProgress() {
        hwndStartTimeMap.put(Thread.currentThread().getName(), System.currentTimeMillis());
    }

}
